package dao.impl;

import dao.FileDao;
import domian.File;
import utils.BaseDAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("all")
public class FileDaoImpl implements FileDao {

    @Override
    public boolean addFile(File file) {
        System.out.println("123");
        String sql = "insert into file (f_title,f_name,f_classId,f_userId,f_describe,f_lable,f_size,f_uploadTime,f_integral)values(?,?,?,?,?,?,?,?,?)";
        Object[] filed = {file.getF_title(), file.getF_name(), file.getF_classId(), file.getF_userId(), file.getF_describe(), file.getF_lable(), file.getF_size(), file.getF_uploadTime(), file.getF_integral()};
        int i = BaseDAO.executeUpdate(sql, filed);
        return i > 0;
    }

    @Override
    public boolean updateFile(File file) {
        String sql = "update file set f_total=?,f_view=? where f_id=?";

        Object[] obj = {file.getF_total(),file.getF_view(),file.getId()};

        int i = BaseDAO.executeUpdate(sql, obj);

        return i > 0;
    }

    /**
     * 按id查询文件信息
     *
     * @param id
     * @return
     */
    @Override
    public File findFileById(Integer id) {
        File file = null;

        String sql = "select * from file where f_id=?";

        Connection conn = BaseDAO.getConnection();

        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            ps = conn.prepareStatement(sql);
            ps.setObject(1, id);
            rs = ps.executeQuery();

            while (rs.next()) {
                file = new File();
                file.setId(rs.getInt("f_id"));
                file.setF_title(rs.getString("f_title"));
                file.setF_name(rs.getString("f_name"));
                file.setF_classId(rs.getInt("f_classid"));
                file.setF_userId(rs.getInt("f_userid"));
                file.setF_describe(rs.getString("f_describe"));
                file.setF_lable(rs.getString("f_lable"));
                file.setF_size(rs.getDouble("f_size"));
                file.setF_total(rs.getInt("f_total"));
                file.setF_view(rs.getInt("f_view"));
                file.setF_uploadTime(rs.getString("f_uploadTime"));
                file.setF_integral(rs.getInt("f_integral"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDAO.close(conn, ps, rs);
        }
        return file;
    }

    //根据文件标题查询文件信息
    @Override
    public File findFileTitle(String title) {
        File file = null;

        String sql = "select * from file where f_title=?";

        Connection conn = BaseDAO.getConnection();

        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            ps = conn.prepareStatement(sql);
            ps.setObject(1, title);
            rs = ps.executeQuery();

            while (rs.next()) {
                file = new File();
                file.setId(rs.getInt("f_id"));
                file.setF_title(title);
                file.setF_name(rs.getString("f_name"));
                file.setF_classId(rs.getInt("f_classid"));
                file.setF_userId(rs.getInt("f_userid"));
                file.setF_describe(rs.getString("f_describe"));
                file.setF_lable(rs.getString("f_lable"));
                file.setF_size(rs.getDouble("f_size"));
                file.setF_total(rs.getInt("f_total"));
                file.setF_view(rs.getInt("f_view"));
                file.setF_uploadTime(rs.getString("f_uploadTime"));
                file.setF_integral(rs.getInt("f_integral"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDAO.close(conn, ps, rs);
        }
        return file;
    }

    @Override
    public List<File> findFilePage(Integer fileClass, Integer currentPage, Integer pageSize) {
        Integer offset = (currentPage - 1) * pageSize;  //分页查询的启示记录

        List<File> list = new ArrayList<>();

        Connection conn = BaseDAO.getConnection();

        String sql = "select * from file ";

        PreparedStatement ps = null;
        ResultSet rs = null;

        if (fileClass != null && !fileClass.equals(0)) {
            sql += "where f_classId=? ORDER BY f_id DESC limit ?,?";
        } else {
            sql += " ORDER BY f_id DESC limit ?,?";
        }
        try {
            ps = conn.prepareStatement(sql);

            if (fileClass != null && !fileClass.equals(0)) {
                ps.setObject(1, fileClass);
                ps.setObject(2, offset);
                ps.setObject(3, pageSize);
            } else {
                ps.setObject(1, offset);
                ps.setObject(2, pageSize);
            }

            rs = ps.executeQuery();

            while (rs.next()) {
                File file = new File();
                file.setId(rs.getInt("f_id"));
                file.setF_title(rs.getString("f_title"));
                file.setF_name(rs.getString("f_name"));
                file.setF_classId(rs.getInt("f_classid"));
                file.setF_userId(rs.getInt("f_userid"));
                file.setF_describe(rs.getString("f_describe"));
                file.setF_lable(rs.getString("f_lable"));
                file.setF_size(rs.getDouble("f_size"));
                file.setF_total(rs.getInt("f_total"));
                file.setF_view(rs.getInt("f_view"));
                file.setF_uploadTime(rs.getString("f_uploadTime"));
                file.setF_integral(rs.getInt("f_integral"));
                list.add(file);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDAO.close(conn, ps, rs);
        }

        return list;
    }

    @Override
    public Integer findCount(Integer fileClass) {
        Integer count = null;

        String sql = "select COUNT(f_id) as count from file ";

        if (fileClass != null && !fileClass.equals(0)) {
            sql += "where f_classId=?";
        }

        Connection conn = BaseDAO.getConnection();

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            if (fileClass != null && !fileClass.equals(0)) {
                ps.setObject(1, fileClass);
            }
            rs = ps.executeQuery();

            while (rs.next()) {
                count = rs.getInt("count");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDAO.close(conn, ps, rs);
        }
        return count;
    }


}
